// RayTracing.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "World.h"
#include "GeometricObjects.h"
#include "Objects.h"
#include <vector>
#include "Point3D.h"
#include "gw.h"
#include "Logger.h"
#include "Camera.h"
#include "Common.h"

//for matlab
//#include <engine.h>
//#include <iostream>
//#include <string>
//#include <cmath>
//
////
//#pragma comment (lib, "libmx.lib")
//#pragma comment (lib, "libmex.lib")
//#pragma comment(lib, "libeng.lib")
//ed of matlab stuff

#define SCRWIDTH	600
#define SCRHEIGHT	600
#define SCENE_TYPE  6
#define CAMERA_DELTA 0.5
#define AMOUNT_FRAMES_TOTAL 10
#define VIEW_PLANE_EYE_DISTANCE 1


int _tmain(int argc, _TCHAR* argv[])
{
	/*Engine::Logger log;

	log.LogInfo("this is a test");*/

	//engine *m_Engine;
	//m_Engine = engOpen("null");

	//
	//if (m_Engine == NULL)
	//{
	//	std::cout << "Error matlab was not found" << std::endl;
	//}
	try
	{
		Engine::World * currentWorld = new Engine::World(SCENE_TYPE, SCRWIDTH, SCRHEIGHT);
		gwOpen("Test Window", SCRWIDTH, SCRHEIGHT);
		//Engine::Camera* currentCamera = new  Engine::Camera(Point3D(-4, 0, VIEW_PLANE_EYE_DISTANCE), Point3D(0, -3, -15));
		//default camera
		//Engine::Camera* currentCamera = new  Engine::Camera(Point3D(0, 0, VIEW_PLANE_EYE_DISTANCE), Point3D(0, -3, -15), 30, 1, 1);
		Engine::Camera* currentCamera = new  Engine::Camera(Point3D(-15,-13,0), Point3D(5,-10,0), 55, 1, 1);
		currentWorld->build(currentCamera);

#ifdef MULTIPLE_FRAMES

		for (int index = 0; index < AMOUNT_FRAMES_TOTAL; index++)
		{
			clock_t startTime = clock();

			while (!currentWorld->render_scene())
			{
				gwDoEvents();
			}

			clock_t endTime = clock();
			double timeInSeconds = ((double)endTime - startTime) / (double)CLOCKS_PER_SEC;

			Engine::Logger::LogTrace("time taken is equal to-> "+to_string(timeInSeconds));

			currentWorld->m_currentY = 0;
			currentCamera->eye.x += CAMERA_DELTA;
		}
#else

		clock_t startTime = clock();

		//while (!currentWorld->render_scene())
		while (!currentWorld->render_scene())
		{
			gwDoEvents();
		}

		currentWorld->currentImage->WriteToFile("tester.ppm");

		clock_t endTime = clock();
		double timeInSeconds = ((double)endTime - startTime) / (double)CLOCKS_PER_SEC;

		Engine::Logger::LogTrace("time taken is equal to-> " + to_string(timeInSeconds));

		currentWorld->m_currentY = 0;

#endif
	}
	catch (exception e)
	{
		CloseWindow();
	}
	Engine::Logger::LogInfo("Render Ready");

	system("pause");
	return 0;
}

